import java.util.LinkedList;
import java.util.Queue;

public class MyStack2 {
    private Queue<Integer> que1;
    private Queue<Integer> que2;
    public MyStack2() {
        que1=new LinkedList<>();
        que2=new LinkedList<>();
    }

    public void push(int x) {
        if (!que1.isEmpty()){
            que1.offer(x);
        } else if (!que2.isEmpty()) {
            que2.offer(x);
        }else {//两者都为空
            que1.offer(x);
        }
    }

    public int pop() {
        if (empty()){
            return -1;
        }
        if(!que1.isEmpty()){//que1不为空
             int len=que1.size();
             for (int i=0;i<len-1;i++){
                 int val=que1.poll();
                 que2.offer(val);
             }
             return que1.poll();
        }
        else {
            int len=que2.size();
            for (int i = 0; i < len-1; i++) {
                int val=que2.poll();
                que1.offer(val);
            }
            return que2.poll();
        }

    }

    public int top() {
        if (empty()){
            return -1;
        }
        if(!que1.isEmpty()){//que1不为空
            int len=que1.size();
            int val=-1;
            for (int i=0;i<len;i++){
                val=que1.poll();
                que2.offer(val);
            }
            return val;
        }
        else {
            int len=que2.size();
            int val=-1;
            for (int i = 0; i < len; i++) {
                val=que2.poll();
                que1.offer(val);
            }
            return val;
        }
    }

    public boolean empty() {
        return que1.isEmpty()&&que2.isEmpty();//两者都为空
    }
}
